package com.ylr.job.biz.impl;

import com.ylr.grpc.ExecutorBiz.ExecutorBizGrpc;
import com.ylr.grpc.ExecutorBiz.ExecutorBizProto;
import com.ylr.job.biz.ExecutorBiz;
import com.ylr.job.biz.model.ReturnT;
import com.ylr.job.biz.model.TriggerParam;
import com.ylr.job.executor.XxlJobExecutor;
import com.ylr.job.util.GsonTool;
import com.ylr.job.util.XxlJobRemotingUtil;
import io.grpc.stub.StreamObserver;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpUtil;
import io.netty.util.CharsetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author ylr
 * @version 1.0.0
 * @ClassName ExecutorBizImpl.java
 * @Description TODO   定义服务端的代码
 * @createTime 2021年11月04日 13:01:00
 */
public class ExecutorBizIProtoImpl extends ExecutorBizGrpc.ExecutorBizImplBase {

    private static Logger logger = LoggerFactory.getLogger(ExecutorBizIProtoImpl.class);


    private ExecutorBiz executorBiz;
    private String accessToken;
    private ThreadPoolExecutor bizThreadPool;

    public ExecutorBizIProtoImpl(ExecutorBiz executorBiz,String accessToken, ThreadPoolExecutor bizThreadPool) {
        this.accessToken = accessToken;
        this.executorBiz = executorBiz;
        this.bizThreadPool = bizThreadPool;
    }

    public ExecutorBizIProtoImpl() {
    }

    @Override
    public void beat(ExecutorBizProto.HelloRequest request, StreamObserver<ExecutorBizProto.ResultReply> responseObserver) {
        // invoke
        bizThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                // do invoke
                ExecutorBizProto.ResultReply reply = ExecutorBizProto.ResultReply.newBuilder()
                        .setCode(200)
                        .setMessage( request.getName() + "Beat Success !")
                        .build();
                responseObserver.onNext(reply);
                responseObserver.onCompleted();
            }
        });


    }

    @Override
    public void idleBeat(ExecutorBizProto.IdleBeatParamRequest request, StreamObserver<ExecutorBizProto.ResultReply> responseObserver) {
        super.idleBeat(request, responseObserver);
    }

    @Override
    public void run(ExecutorBizProto.TriggerParamRequest triggerParamProto, StreamObserver<ExecutorBizProto.ResultReply> responseObserver) {
        // invoke
        TriggerParam triggerParam = new TriggerParam();
        triggerParam.setJobId(triggerParamProto.getJobId());
        triggerParam.setExecutorHandler(triggerParamProto.getExecutorHandler());
        triggerParam.setExecutorParams(triggerParamProto.getExecutorParams());
        triggerParam.setExecutorBlockStrategy(triggerParamProto.getExecutorBlockStrategy());
        triggerParam.setExecutorTimeout(triggerParamProto.getExecutorTimeout());
        triggerParam.setLogId(triggerParamProto.getLogId());
        triggerParam.setLogDateTime(triggerParamProto.getLogDateTime());
        triggerParam.setGlueType(triggerParamProto.getGlueType());
        triggerParam.setGlueSource(triggerParamProto.getGlueSource());
        triggerParam.setGlueUpdatetime(triggerParamProto.getGlueUpdatetime());
        triggerParam.setBroadcastIndex(triggerParamProto.getBroadcastIndex());
        triggerParam.setBroadcastTotal(triggerParamProto.getBroadcastTotal());
        bizThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                // do invoke
                ReturnT<String> result = null;
                try {
                    result = executorBiz.run(triggerParam);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                ExecutorBizProto.ResultReply reply = ExecutorBizProto.ResultReply.newBuilder()
                        .setCode(200)
                        .setContent(GsonTool.toJson(result))
                        .build();
                responseObserver.onNext(reply);
                responseObserver.onCompleted();
            }
        });

    }

    @Override
    public void kill(ExecutorBizProto.KillParamRequest request, StreamObserver<ExecutorBizProto.ResultReply> responseObserver) {
        super.kill(request, responseObserver);
    }

    @Override
    public void log(ExecutorBizProto.LogParamParamRequest request, StreamObserver<ExecutorBizProto.ResultReply> responseObserver) {
        super.log(request, responseObserver);
    }
}
